package com.ukao.pad.printer;

import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.example.tscdll.TSCUSBActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ukao.pad.R;
import com.ukao.pad.bean.AllMarkprintBean;
import com.ukao.pad.bean.MarkprintBean;
import com.ukao.pad.bean.PrinterBean;
import com.ukao.pad.bean.ReceiptBean;
import com.ukao.pad.bean.StrikeBean;
import com.ukao.pad.bean.StringBean;
import com.ukao.pad.listener.OnPrintListener;
import com.ukao.pad.utils.CheckUtils;
import com.ukao.pad.utils.DateUtils;
import com.ukao.pad.utils.GsonUtil;
import com.ukao.pad.utils.L;
import com.ukao.pad.utils.T;
import com.ukao.pad.utils.Utils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import static com.ukao.pad.R.id.flawDesc;
import static com.ukao.pad.R.id.orderPrice;
import static com.ukao.pad.R.id.productName;

/**
 * Created by herego on 2017/6/26.
 * 注意打印机需要刷字体
 * Font001 : 新宋体  小五
 * Font002:  新宋体  四号
 * 打印合格证
 */
public class PrinterHelper {
    private static PrinterHelper mPrinterHelper = null;
    private UsbManager mUsbManager = null;
    private AllMarkprintBean mClotbean;
    private Handler mHandler=new Handler();
    private int countall;
    private int count;
    private  String  productName;
    private List<AllMarkprintBean.ProductListBean.AnnexListBean> productLists;
    private List<StringBean> mAnnexDesclist;
    private List<MarkprintBean.AnnexListBean> productList;

    private PrinterHelper( ){
        mUsbManager=(UsbManager)Utils.getContext().getSystemService(Context.USB_SERVICE);

    }

    public static PrinterHelper getInstance( ){
        if(mPrinterHelper == null) {
            mPrinterHelper = new PrinterHelper();
        }
        return  mPrinterHelper;
    }

    public byte[] convertGBK(String str){
        byte[] retBytes = null;
        try {
            retBytes = str.getBytes("GBK");
        }catch (UnsupportedEncodingException ex){
            ex.printStackTrace();
        }
        return retBytes;
    }

    public UsbDevice getSpecialPrinter(int vendorId, int productId){
        Log.i("Debug:","getSpecialPrinter()  vendorId:"+ vendorId+" productId"+productId);
        mUsbManager=(UsbManager)Utils.getContext().getSystemService(Context.USB_SERVICE);
        if(mUsbManager != null) {
            Log.i("Debug:","getSpecialPrinter()");
            HashMap<String, UsbDevice> deviceHashMap = mUsbManager.getDeviceList();
            Iterator<UsbDevice> iterator = deviceHashMap.values().iterator();
            Log.i("Debug:", "onCreate(Bundle savedInstanceState) ");
            while (iterator.hasNext()) {
                UsbDevice device = iterator.next();
                Log.i("Debug:", "  device name: " + device.getDeviceName() + "  device product name:"
                        + "   vendor id:" + device.getVendorId());
                if (device.getVendorId() == vendorId && device.getProductId() == productId) {
                    return device;
                }
            }
        }else {
            Log.i("Debug:","-------------usbManager is null");
        }
        return null;
    }

    /**
     * 打印 型号Tsc244
     * @param printInfo
     */

    public void printTsc244(PrintInfo printInfo){
        UsbDevice usbDevice = getSpecialPrinter(4611,370);
        if(usbDevice == null){
            System.out.println("usbDevice is null");
            return;
        }

        try {
            TSCUSBActivity tscusbActivity = new TSCUSBActivity();
            try {
                tscusbActivity.openport(mUsbManager, usbDevice);
            }catch (Exception  e){
                T.show(Utils.getContext().getString(R.string.default_use_usb_device_inot_set));
                return;
            }

            tscusbActivity.sendcommand("SIZE 77.5 mm, 40 mm\n");
            tscusbActivity.sendcommand("DIRECTION 0,0\n");
            tscusbActivity.sendcommand("REFERENCE 0,0\n");
            tscusbActivity.sendcommand("DIRECTION 1\n");
            //tscusbActivity.sendcommand("SET RIBBON OFF\n"); /**/  ON 热转印模式      OFF热感印模式
            tscusbActivity.sendcommand("CLS\n");
            tscusbActivity.sendcommand("GAP 3 mm,0 mm\n");
            tscusbActivity.sendcommand("OFFSET 0 mm\n");
            tscusbActivity.sendcommand("SET PEEL OFF\n");
            tscusbActivity.sendcommand("SET CUTTER OFF\n");
            tscusbActivity.sendcommand("SET PARTIAL_CUTTER OFF\n");
//       tscusbActivity.sendcommand(convertGBK("BARCODE 20,110,\"128\",1,0,0,2,2,\""+printInfo.mClothesBarcode+"\"\n"))  / 第一个x y  *128高度*/
            tscusbActivity.sendcommand(convertGBK("TEXT 5,10,\"FONT002\",0,1,1,\""+printInfo.mGoodsName+"\"\n"));
            if(!TextUtils.isEmpty(printInfo.colorName)){
                tscusbActivity.sendcommand(convertGBK("TEXT 180,10,\"FONT002\",0,1,1,\""+printInfo.colorName+"\"\n"));  //打印颜色名字
            }
            tscusbActivity.sendcommand(convertGBK("TEXT 5,30,\"FONT002\",0,1,1,\"单号："+printInfo.mOrderNum+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 5,50,\"FONT002\",0,1,1,\"数量：衣物"+ printInfo.productCnt+",附件"+printInfo.annexCount+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 5,70,\"FONT002\",0,1,1,\"收衣时间："+printInfo.mReciveClothesDatetime+"\"\n"));
             String strThree=printInfo.mClothesBarcode.substring(0,3);
            tscusbActivity.sendcommand(convertGBK("BARCODE 1,110,\"128\",170,0,0,2,2,\""+strThree+"\"\n"));
//            tscusbActivity.sendcommand(convertGBK("TEXT 5,230,\"FONT001\",0,2,2,\""+strThree+"\"\n"));

            tscusbActivity.sendcommand(convertGBK("TEXT 290,120,\"FONT001\",0,2,2,\"合\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 290,150,\"FONT001\",0,2,2,\"格\"\n"));
            tscusbActivity.sendcommand(convertGBK("BOX 285,110,325,180,2\n"));
            if (printInfo.mShopName.length()>8){
                printInfo.mShopName=printInfo.mShopName.substring(0,8)+"...";

            }

            tscusbActivity.sendcommand(convertGBK("TEXT 300,26,\"FONT002\",0,1,1,\""+printInfo.mShopName+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 505,10,\"FONT002\",0,2,2,\""+printInfo.mShopNum+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 445,66,\"FONT002\",0,1,1,\""+printInfo.mTransportName+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 505,50,\"FONT002\",0,2,2,\""+printInfo.mTransportNum+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("BARCODE 370,110,\"128\",120,0,0,2,1,\""+printInfo.mClothesBarcode+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 370,230,\"FONT001\",0,2,2,\""+printInfo.mClothesBarcode+"\"\n"));


            tscusbActivity.sendcommand("PRINT 1\n");
        }catch (Exception ex){
            T.show(Utils.getContext().getString(R.string.printer_error));
        }
    }







    /**
     * 打印合格标
     * @param mStrikeBean
     */
  public  void  printStrike(StrikeBean mStrikeBean){
      PrintInfo printInfo = new PrintInfo();
      printInfo.mGoodsName = mStrikeBean.getProductName();
      printInfo.mOrderNum = mStrikeBean.getOrderNo();
      printInfo.productCnt= mStrikeBean.getProductCnt();  //衣服数量
      printInfo.mReciveClothesDatetime = DateUtils.formatDateCustom(mStrikeBean.getCreateTime(),"MM-dd HH:mm:ss");
      printInfo.mClothesBarcode =mStrikeBean.getScanCode();
      printInfo.mShopName =mStrikeBean.getStoreName();
      printInfo.mShopNum = mStrikeBean.getStoreId()+"";
      printInfo.mTransportNum = mStrikeBean.getTranNum()+"";
      printInfo.mTransportName=mStrikeBean.getTranText();
      printInfo.annexCount=mStrikeBean.getAnnexCnt();  //衣服附件的数量
      if(!TextUtils.isEmpty(mStrikeBean.getColorDesc())){
          //Gson解析
          Gson gson=  GsonUtil.getInstance();
          List<StringBean> mColorDescList = gson.fromJson(mStrikeBean.getColorDesc(),
                  new TypeToken<List<StringBean>>() {
                  }.getType());
            if(mColorDescList.size()>0){
                StringBean stringBean=mColorDescList.get(0);
                printInfo.colorName=stringBean.getName();
            }
      }
      mPrinterHelper.printTsc244(printInfo);
  }






    public   void   printAllWashings(AllMarkprintBean mClotbean){
        this.mClotbean=mClotbean;
        countall=0;
        printAllWashing();
    }

    public   void   printAllWashing(  ){
        List<AllMarkprintBean.ProductListBean> allproductList = mClotbean.getProductList();
        if (CheckUtils.isEmpty(allproductList))return;
        if (allproductList.size()-1<countall)return;
        AllMarkprintBean.ProductListBean mitem = allproductList.get(countall);
        final     PrintInfo printInfo = new PrintInfo();
        Gson mGson = new Gson();
        String color="";
        if (!CheckUtils.isEmpty(mitem.getColorDesc())) {  /*/*瑕疵*/
            List<StringBean> mColorDesclist = mGson.fromJson(mitem.getColorDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());
            StringBuffer mColorBuffer1 = new StringBuffer();
            for (int i = 0; i < mColorDesclist.size(); i++) {
                if (i != mColorDesclist.size() - 1) {
                    mColorBuffer1.append(mColorDesclist.get(i).getName() + "/");
                } else {
                    mColorBuffer1.append(mColorDesclist.get(i).getName());
                }


            }
            color= mColorBuffer1.toString();
        }
        String strflawDesc = "";
        if (!CheckUtils.isEmpty(mitem.getFlawDesc())) {  /*/*瑕疵*/
            List<StringBean> mflawDesclist = mGson.fromJson(mitem.getFlawDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());
            StringBuffer mflawBuffer = new StringBuffer();
            for (int i = 0; i < mflawDesclist.size(); i++) {
                if (i != mflawDesclist.size() - 1) {
                    mflawBuffer.append(mflawDesclist.get(i).getName() + "/");
                } else {
                    mflawBuffer.append(mflawDesclist.get(i).getName());
                }


            }
            strflawDesc = mflawBuffer.toString();
        }
        String annexDesc = "";
        int annexDescCount=0;
        if (!CheckUtils.isEmpty(mitem.getAnnexDesc())){  /*附件*/
            mAnnexDesclist= mGson.fromJson(mitem.getAnnexDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());

            StringBuffer mStringBuffer3 = new StringBuffer();
            for (int i = 0; i < mAnnexDesclist.size(); i++) {
                annexDescCount=annexDescCount+1;
                if (i != mAnnexDesclist.size() - 1) {
                    mStringBuffer3.append(mAnnexDesclist.get(i).getName() + "/");
                } else {
                    mStringBuffer3.append(mAnnexDesclist.get(i).getName());
                }


            }
            annexDesc = mStringBuffer3.toString();

        }



        String service = "";
        if (!CheckUtils.isEmpty(mitem.getAddServiceDesc())){  /* 附加  */
            List<StringBean> mServiceDesclist = mGson.fromJson(mitem.getAddServiceDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());

            StringBuffer mserviceBuffer = new StringBuffer();
            for (int i = 0; i < mServiceDesclist.size(); i++) {
                StringBean  item=mServiceDesclist.get(i);
                if (i==0){
                    mserviceBuffer.append("/");
                }
                if (i != mServiceDesclist.size() - 1) {
                    mserviceBuffer.append(item.getName()+ "/");
                } else {
                    mserviceBuffer.append(item.getName());
                }


            }
            service=mserviceBuffer.toString();

        }
        /*最一层*/
        printInfo.createTime=mitem.getCreateTime();
        String  userName=mClotbean.getUserName();
        printInfo.Use = mClotbean.getUserPhone()+CheckUtils.isEmptyStringLeng(userName,4);
        printInfo.mTotalCount = mClotbean.getProductCnt()+"";
        printInfo.mShopName =CheckUtils.isEmptyString(mClotbean.getStoreName());
        printInfo.mShopNum = CheckUtils.isEmptyString(mClotbean.getStoreId()+"");/*门店id*/
        printInfo.mOrderInfo = "*"+mClotbean.getOrderNo().substring(mClotbean.getOrderNo().length()-6);/*订单*/
        /*第二层*/
        String   subtotal=CheckUtils.isEmptyNumber(mitem.getSubtotal());
      String  paystate=mClotbean.isPaid() ? "  已" :"  未";
        productName=CheckUtils.isEmptyStringLeng(mitem.getProductName(),7);
        printInfo.mGoodsName = productName+subtotal+paystate;/*小计*/
        printInfo.mCurrentIndex = mitem.getNo()+"";
        printInfo.isaffix=mitem.getType()==1?true :false;/*是否是衣物*/
        printInfo.mcleanout =mitem.getServiceName()+CheckUtils.isEmptyStringLeng(service,14);  /*服务与 附加服务*/
        printInfo.mTransportNum =mitem.getTranNum()+""; /*输送线*/
        printInfo.mClothesBarcode =mitem.getScanCode();/*二维码*/
        printInfo.annexDescCount=annexDescCount;
        printInfo.mTransportName= CheckUtils.isEmptyString(mitem.getTranText());
        printInfo.mAttachGoods =annexDescCount+CheckUtils.isEmptyStringLeng(annexDesc,20) ;  /*附件*/
        printInfo.mAttachInfo =color+"  "+CheckUtils.isEmptyStringLeng(strflawDesc,12);/*瑕疵*//*衣物*/
        count=-1;
        productLists= mitem.getAnnexList();
        mPrinterHelper.printTsc247(printInfo, new OnPrintListener() {
            @Override
            public void onPrintSucced() {
                if (CheckUtils.isEmpty(productLists)){ /*没附件*/
                    countall=countall+1;
                    printAllWashing();

                }else{
                    printAttachment(printInfo);
                }

            }

            @Override
            public void onPrintFail() {
                if (CheckUtils.isEmpty(productLists)){ /*没附件*/
                    countall=countall+1;
                    printAllWashing();

                }else{
                    printAttachment(printInfo);
                }
            }
        });


    }
    public void printAttachment(final PrintInfo printInfo) {
    /*附件集合*/
        count=count+1;
        if (productLists.size()-1>=count) {
            printInfo.isaffix=false;
            printInfo.mAttachInfo="附件:"+productLists.get(count).getProductName();
            printInfo.mClothesBarcode =productLists.get(count).getScanCode();
            printInfo.mCurrentIndex=productLists.get(count).getNo()+"";
            String    subtotal=" "+ CheckUtils.isEmptyNumber(productLists.get(count).getSubtotal());
        String    paystate=mClotbean.isPaid() ? "  已" :"  未";
            printInfo.mGoodsName =productName+subtotal+paystate;/*小计*/
            mPrinterHelper.printTsc247(printInfo, new OnPrintListener() {
                @Override
                public void onPrintSucced() {
                    printAttachment(printInfo);

                }

                @Override
                public void onPrintFail() {
                    printAttachment(printInfo);
                }
            });

        }else {
            countall=countall+1;
            printAllWashing();
        }
    }




    public void printTsc247(PrintInfo printInfo, final OnPrintListener   mOnPrintListener){

        UsbDevice  usbDevice=null;
        if (CheckPrinterHelper.isT210EWashprinter()){
            usbDevice = getSpecialPrinter(4611,310);
        }else{
            usbDevice = getSpecialPrinter(4611,294);
        }
        if(usbDevice == null){
            System.out.println("usbDevice is null");
            return;
        }

        try {
            TSCUSBActivity tscusbActivity = new TSCUSBActivity();
            try {
                tscusbActivity.openport(mUsbManager, usbDevice);
            }catch (Exception  e){
                T.show(Utils.getContext().getString(R.string.default_use_usb_device_inot_set));
                return;
            }

            tscusbActivity.sendcommand("SIZE 110 mm,15 mm\n");
            tscusbActivity.sendcommand("GAP 0 mm,0 mm\n");
            tscusbActivity.sendcommand("DIRECTION 1\n");
            tscusbActivity.sendcommand("CLS\n");
            tscusbActivity.sendcommand("SET CUTTER ON\n");

            if (printInfo.isaffix){  /*是否是衣物*/
                tscusbActivity.sendcommand(convertGBK("TEXT 65,85,\"FONT001\",0,2,2,\""+printInfo.mAttachInfo+"\"\n"));
            }else{

                tscusbActivity.sendcommand(convertGBK("TEXT 65,85,\"FONT001\",0,3,3,\""+ printInfo.mAttachInfo+"\"\n"));
            }
            tscusbActivity.sendcommand(convertGBK("TEXT 20,38,\"FONT001\",0,2,2,\""+printInfo.mCurrentIndex+"\"\n"));/*当前数*/
            tscusbActivity.sendcommand(convertGBK("TEXT 20,85,\"FONT001\",0,2,2,\""+printInfo.mTotalCount+"\"\n"));/*总数*/
            tscusbActivity.sendcommand(convertGBK("TEXT 20,8,\"FONT001\",0,2,2,\""+printInfo.mGoodsName+"\"\n"));/*衣物*/
            tscusbActivity.sendcommand(convertGBK("TEXT 340,8,\"FONT001\",0,2,2,\""+printInfo.mOrderInfo+"\"\n"));/*订单号*/
            tscusbActivity.sendcommand(convertGBK("TEXT 65,35,\"FONT001\",0,2,2,\""+printInfo.mcleanout+"\"\n"));/*清洗*/
            if (printInfo.annexDescCount!=0){
                tscusbActivity.sendcommand(convertGBK("TEXT 65,60,\"FONT001\",0,2,2,\"附件："+printInfo.mAttachGoods+"\"\n"));/*附件数量*/
            }
            tscusbActivity.sendcommand(convertGBK("TEXT 20,65,\"FONT001\",0,2,2,\"-\"\n"));

            printInfo.mTransportName=CheckUtils.isEmptyStringLeng(printInfo.mTransportName,1);
            String   time=printInfo.mClothesBarcode+" "+DateUtils.formatDateCustom(printInfo.createTime,"MM.dd");
            tscusbActivity.sendcommand(convertGBK("QRCODE 490,8,M,4,A,0,M2,\""+printInfo.mClothesBarcode+"\"\n")); /*二维码*/
            tscusbActivity.sendcommand(convertGBK("TEXT 590,20,\"FONT001\",0,2,2,\""+printInfo.mShopName+"\"\n"));/*店铺*/
            tscusbActivity.sendcommand(convertGBK("TEXT 810,20,\"0\",0,14,13,\""+printInfo.mShopNum+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 590,60,\"FONT001\",0,2,2,\""+time+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 490,90,\"FONT001\",0,2,2,\""+printInfo.Use+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 730,90,\"FONT001\",0,2,2,\""+printInfo.mTransportName+"\"\n"));
            tscusbActivity.sendcommand(convertGBK("TEXT 750,90,\"0\",0,14,13,\""+printInfo.mTransportNum+"\"\n"));/*输送线号*/
            tscusbActivity.sendcommand("PRINT 1\n");

            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mOnPrintListener.onPrintSucced();
                }
            },900);

        }catch (Exception ex){
            T.show(Utils.getContext().getString(R.string.printer_error));
//           mHandler.postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    mOnPrintListener.onPrintFail();
//                }
//            },600);


        }
    }



    /**
     * 单独打标
     * @param mClotbean
     */
    MarkprintBean mthoClotbean;
    public   void   printWashing(MarkprintBean mClotbean){
        this.mthoClotbean=mClotbean;
        final     PrintInfo printInfo = new PrintInfo();

        Gson mGson = new Gson();
        String color="";
        if (!CheckUtils.isEmpty(mClotbean.getColorDesc())) {  /*/*瑕疵*/
            List<StringBean> mColorDesclist = mGson.fromJson(mClotbean.getColorDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());
            StringBuffer mColorBuffer1 = new StringBuffer();
            for (int i = 0; i < mColorDesclist.size(); i++) {
                if (i != mColorDesclist.size() - 1) {
                    mColorBuffer1.append(mColorDesclist.get(i).getName() + "/");
                } else {
                    mColorBuffer1.append(mColorDesclist.get(i).getName());
                }
            }
            color= mColorBuffer1.toString();
        }
        String strflawDesc = "";
        if (!CheckUtils.isEmpty(mClotbean.getFlawDesc())) {  /*/*瑕疵*/
            List<StringBean> mflawDesclist = mGson.fromJson(mClotbean.getFlawDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());
            StringBuffer mflawBuffer = new StringBuffer();
            for (int i = 0; i < mflawDesclist.size(); i++) {
                if (i != mflawDesclist.size() - 1) {
                    mflawBuffer.append(mflawDesclist.get(i).getName() + "/");
                } else {
                    mflawBuffer.append(mflawDesclist.get(i).getName());
                }


            }
            strflawDesc = mflawBuffer.toString();
        }
        String annexDesc = "";
        int annexDescCount=0;
        if (!CheckUtils.isEmpty(mClotbean.getAnnexDesc())){  /*附件*/
            mAnnexDesclist= mGson.fromJson(mClotbean.getAnnexDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());

            StringBuffer mStringBuffer3 = new StringBuffer();
            for (int i = 0; i < mAnnexDesclist.size(); i++) {
                annexDescCount=annexDescCount+1;
                if (i != mAnnexDesclist.size() - 1) {
                    mStringBuffer3.append(mAnnexDesclist.get(i).getName() + "/");
                } else {
                    mStringBuffer3.append(mAnnexDesclist.get(i).getName());
                }


            }
            annexDesc = mStringBuffer3.toString();

        }


        String service = "";
        if (!CheckUtils.isEmpty(mClotbean.getAddServiceDesc())){  /* 附加  */
            List<StringBean> mServiceDesclist = mGson.fromJson(mClotbean.getAddServiceDesc(),
                    new TypeToken<List<StringBean>>() {
                    }.getType());

            StringBuffer mserviceBuffer = new StringBuffer();
            for (int i = 0; i < mServiceDesclist.size(); i++) {
                StringBean  item=mServiceDesclist.get(i);
                if (i==0){
                    mserviceBuffer.append("/");
                }
                if (i != mServiceDesclist.size() - 1) {
                    mserviceBuffer.append(item.getName()+ "/");
                } else {
                    mserviceBuffer.append(item.getName());
                }


            }
            service=mserviceBuffer.toString();

        }


        String  subtotal="  "+CheckUtils.isEmptyNumber(mClotbean.getSubtotal());/*小计*/
        String    paystate=mClotbean.isPaid() ? "  已" :"  未";

        if (mClotbean.getType()){
            printInfo.mGoodsName =  CheckUtils.isEmptyStringLeng(mClotbean.getProductName(),7)+subtotal+paystate;
            printInfo.mAttachInfo =color+"  "+CheckUtils.isEmptyStringLeng(strflawDesc,12);/*瑕疵*//*衣物*/
        }else{

            printInfo.mGoodsName =  CheckUtils.isEmptyStringLeng(mClotbean.getParentProName(),7)+subtotal+paystate;
            printInfo.mAttachInfo="附件:"+mClotbean.getProductName();
        }
        printInfo.mOrderInfo = "*"+mClotbean.getOrderNo().substring(mClotbean.getOrderNo().length()-6);/*订单*/
        printInfo.mcleanout =mClotbean.getServiceName()+CheckUtils.isEmptyStringLeng(service,20);  /*服务与 附加服务 14*/
        printInfo.mCurrentIndex = mClotbean.getNo()+"";
        printInfo.mTotalCount = mClotbean.getProductCnt()+"";
        printInfo.annexDescCount=annexDescCount;
        printInfo.mAttachGoods =annexDescCount+CheckUtils.isEmptyStringLeng(annexDesc,13) ;  /*附件数量*/
        printInfo.mShopName =CheckUtils.isEmptyString(mClotbean.getStoreName());/*门店*/
        printInfo.mShopNum = CheckUtils.isEmptyString(mClotbean.getStoreId()+"");/*门店id*/
        printInfo.mTransportName= CheckUtils.isEmptyString(mClotbean.getTranText());/*输送线名称*/
        printInfo.mTransportNum =mClotbean.getTranNum()+""; /*输送线号*/
        String  userName=mClotbean.getUserName();
        printInfo.Use = mClotbean.getUserPhone()+CheckUtils.isEmptyStringLeng(userName,4);
        printInfo.mClothesBarcode =mClotbean.getScanCode();
        printInfo.createTime=mClotbean.getCreateTime();
        printInfo.isaffix=mClotbean.getType();/*是否是衣物*/
        productList=mClotbean.getAnnexList();
        count=-1;
        mPrinterHelper.printTsc247(printInfo, new OnPrintListener(){
            @Override
            public void onPrintSucced() {
                postDelayedPrin(printInfo);
            }

            @Override
            public void onPrintFail() {
            }
        });

    }

    /**
     *
     * @param printInfo
     *
     */
    private void postDelayedPrin(final PrintInfo printInfo    ) {
        if (!CheckUtils.isEmpty(productList)){
            count=count+1;
            if (productList.size()-1>=count) {
                printInfo.isaffix=false;
                printInfo.mAttachInfo="附件:"+productList.get(count).getProductName();
                printInfo.mClothesBarcode =productList.get(count).getScanCode();
                printInfo.mCurrentIndex=productList.get(count).getNo()+"";
                String    subtotal="  "+CheckUtils.isEmptyNumber(productList.get(count).getSubtotal());
                String  paystate=mthoClotbean.isPaid() ? "  已" :"  未";
                String productName = mthoClotbean.getProductName();
                String strproductName =CheckUtils.isEmptyStringLeng(productName, 7);
                printInfo.mGoodsName =strproductName+subtotal+paystate;/*小计*/
                mPrinterHelper.printTsc247(printInfo, new OnPrintListener() {
                    @Override
                    public void onPrintSucced() {
                        postDelayedPrin(printInfo);
                    }

                    @Override
                    public void onPrintFail() {
                        postDelayedPrin(printInfo);
                    }
                });

            }


        }
    }


    /**
     * 打印Tsc 小票
     *
     */
    public void printTsc247Receipt(ReceiptBean printInfo){

        UsbDevice  usbDevice=null;
        if (CheckPrinterHelper.isT210EWashprinter()){
            usbDevice = getSpecialPrinter(4611,310);
        }else{
            usbDevice = getSpecialPrinter(4611,294);
        }
        if(usbDevice == null){
            System.out.println("usbDevice is null");
            return;
        }

        try {
            TSCUSBActivity tscusbActivity = new TSCUSBActivity();
            tscusbActivity.openport(mUsbManager, usbDevice);
            tscusbActivity.sendcommand("SIZE 110 mm,35 mm\n");
            tscusbActivity.sendcommand("GAP 0 mm,0 mm\n");
            tscusbActivity.sendcommand("DIRECTION 1\n");
            tscusbActivity.sendcommand("CLS\n");
            tscusbActivity.sendcommand("SET CUTTER ON\n");


           String  recordTime="入库 :" + DateUtils.formatDateCustom(printInfo.getRecoredTime(),"MM-dd HH:mm:ss");
            String  userName="";
            if (!CheckUtils.isEmpty(printInfo.getUserName())){
                userName="  "+printInfo.getUserName();
            }
            if (!CheckUtils.isEmpty(printInfo.getUserPhone())){
                userName=printInfo.getUserPhone()+userName;
            }
            userName=CheckUtils.isEmptyStringLeng(userName,20);
        String    sendAddress=printInfo.getSendAddress();
          String  sendAddress1="";
            String  sendAddress2="";
            if (sendAddress.length()>34){
                 sendAddress1= CheckUtils.isEmptyStringLeng(sendAddress, 34);
                sendAddress2=sendAddress.substring(34,sendAddress.length());
            }else{
                sendAddress1=sendAddress;
            }
    /*送件方式*/
           String sendModeText=printInfo.getSendModeText();
            if (printInfo.getSendMode()==2){
                sendModeText=sendModeText+":"+printInfo.getSendWorkName();
                sendModeText=CheckUtils.isEmptyStringLeng(sendModeText,9);
            }

            String  payPaidPrice="";
            String  paySate="";
            if (printInfo.getPayStatus()){/* 已支付金额*/
                paySate="已";
                payPaidPrice="已付："+CheckUtils.isEmptyNumber(printInfo.getPaidPrice());
            }else{
                paySate="未";
                payPaidPrice="应付："+CheckUtils.isEmptyNumber(printInfo.getPaymentPrice());
            }


            String  storeName=CheckUtils.isEmptyStringLeng(printInfo.getStoreName(),14);
            tscusbActivity.sendcommand(convertGBK("TEXT 10,8,\"FONT001\",0,2,2,\""+printInfo.getMercName()+"\"\n")); /* 商户名称*/
            tscusbActivity.sendcommand(convertGBK("TEXT 10,45,\"FONT001\",0,2,2,\""+storeName+"\"\n"));/*门店*/
            tscusbActivity.sendcommand(convertGBK("TEXT 10,75,\"FONT001\",0,2,2,\""+recordTime+"\"\n"));/*入库时间*/
            tscusbActivity.sendcommand(convertGBK("TEXT 10,115,\"FONT001\",0,2,2,\""+"客户："+userName+"\"\n"));/*客户*/
            tscusbActivity.sendcommand(convertGBK("TEXT 10,150,\"FONT001\",0,2,2,\""+ sendAddress1+"\"\n"));/*地址*/
            tscusbActivity.sendcommand(convertGBK("TEXT 10,185,\"FONT001\",0,2,2,\""+sendAddress2+"\"\n"));/*地址*/
            tscusbActivity.sendcommand(convertGBK("TEXT 10,225,\"FONT001\",0,2,2,\""+sendModeText+"\"\n"));/*送件方式*/
            tscusbActivity.sendcommand(convertGBK("TEXT 10,255,\"FONT001\",0,2,2,\""+"打印"+DateUtils.getDateTime()+"\"\n"));/*打印时间*/


            tscusbActivity.sendcommand(convertGBK("BARCODE 400,8,\"128\",60,2,0,2,2,\""+printInfo.getOrderNo()+"\"\n")); /*订单号*/
            tscusbActivity.sendcommand(convertGBK("QRCODE 710,8,M,6,A,0,M2,\""+printInfo.getOrderNo()+"\"\n")); /*二维码*/
            tscusbActivity.sendcommand(convertGBK("TEXT 470,115,\"FONT001\",0,2,2,\""+printInfo.getClothCnt()+printInfo.getAnnexCnt()+"\"\n"));/*件数*/
            tscusbActivity.sendcommand(convertGBK("TEXT 300,225,\"FONT001\",0,2,2,\""+payPaidPrice+"\"\n"));/*应付*/
            tscusbActivity.sendcommand(convertGBK("TEXT 500,225,\"FONT001\",0,2,2,\""+paySate+"\"\n"));/*付款状态*/
            tscusbActivity.sendcommand(convertGBK("TEXT 465,255,\"FONT001\",0,2,2,\""+"客户签名:"+"\"\n"));


            tscusbActivity.sendcommand("PRINT 1\n");
        }catch (Exception ex){
            System.out.println(ex.getStackTrace());
        }
    }




    public  void  onDestroy(){
        mHandler.removeCallbacksAndMessages(null);
    }
}
